<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input type="number" placeholder="价格(元)" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="重量(g)">
      <el-input type="number" placeholder="重量(g)" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input type="textarea" placeholder="SKU描述" v-model="skuParams.skuDesc"></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form label-width="100px" :inline="true">
        <el-form-item v-for="(item, index) in attrArr" :key="item.id" :label="item.attrName">
          <el-select
            v-model="item.attrIdAndValueId"
            placeholder="请选择"
            clearable
            style="width: 200px"
          >
            <el-option
              :value="`${item.id}:${attrValue.id}`"
              v-for="(attrValue, index1) in item.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item style="margin-top: 40px" label="销售属性">
      <el-form label-width="100px" :inline="true">
        <el-form-item v-for="(item, index) in saleArr" :key="item.id" :label="item.saleAttrName">
          <el-select
            v-model="item.saleIdAndValueId"
            placeholder="请选择"
            clearable
            style="width: 200px"
          >
            <el-option
              :value="`${item.id}:${spuSaleAttrValue.id}`"
              v-for="(spuSaleAttrValue, index1) in item.spuSaleAttrValueList"
              :key="spuSaleAttrValue.id"
              :label="spuSaleAttrValue.saleAttrValueName"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="imgTable">
        <el-table-column type="selection" align="center" width="80" />
        <el-table-column label="图片" align="center">
          <template #="{ row, $index }">
            <img :src="row.imgUrl" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column label="名称" align="center" prop="imgName"></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template #="{ row, $index }">
            <el-button size="small" v-if="row.isDefault == 1"
              ><span style="color: green">默认</span></el-button
            >
            <el-button v-else type="primary" size="small" @click="handler(row)">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { reqAttr } from '@/api/product/attr'
import { reqSpuHasSaleAttr, reqSpuImageList, reqAddSku } from '@/api/product/spu'
import type { SpuData, SkuData, SpuImg } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'

let $emit = defineEmits(['changeScene'])

//子组件点击取消按钮，通知父组件切换场景为0，展示已有的SPU数据
const cancel = () => {
  $emit('changeScene', { flag: 0, params: '' }) //params为空就可以，不需要让父组件再发请求
}

//存储平台属性
let attrArr = ref<any>([])
//销售属性
let saleArr = ref<any>([])
//照片列表
let imgArr = ref<any>([])
//收集sku参数
let skuParams = reactive<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '',
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: ''
})

//获取图片列表组件vc
let imgTable = ref<any>()

//当前子组件的方法对外暴露
const initSkuData = async (c1Id: number | string, c2Id: number | string, spu: SpuData) => {
  //先清空数据
  attrArr.value = []
  saleArr.value = []
  imgArr.value = []
  Object.assign(skuParams, {
    category3Id: '',
    spuId: '',
    tmId: '',
    skuName: '',
    price: '',
    weight: '',
    skuDesc: '',
    skuAttrValueList: [],
    skuSaleAttrValueList: [],
    skuDefaultImg: ''
  })

  //收集数据
  skuParams.category3Id = spu.category3Id
  skuParams.spuId = spu.id as number
  skuParams.tmId = spu.tmId

  //获取平台属性
  let result: any = await reqAttr(c1Id, c2Id, spu.category3Id)
  //获取对应的销售属性
  let result1: any = await reqSpuHasSaleAttr(spu.id as number)
  //获取对应的图片列表
  let result2: any = await reqSpuImageList(spu.id as number)

  attrArr.value = result.data
  saleArr.value = result1.data
  imgArr.value = result2.data
}

//设置默认图片的方法回调
const handler = (row: SpuImg) => {
  //点击的时候，先让全部复选框不勾选
  imgArr.value.forEach((item: any) => {
    imgTable.value.toggleRowSelection(item, false)
  })
  //复选框选中
  imgTable.value.toggleRowSelection(row, true)

  imgArr.value.forEach((item: any) => {
    item.isDefault = 0
  })
  row.isDefault = 1

  skuParams.skuDefaultImg = row.imgUrl as string
}
//保存按钮的方法
const save = async () => {
  //整理参数
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId
      })
    }
    return prev
  }, [])

  skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
      prev.push({
        saleAttrId,
        saleAttrValueId
      })
    }
    return prev
  }, [])
  //发送请求
  let result = await reqAddSku(skuParams)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '添加sku成功'
    })
    //通知父组件切换场景为0
    $emit('changeScene', { flag: 0, params: '' }) //params为空就可以，不需要让父组件再发请求
  } else {
    ElMessage({
      type: 'error',
      message: '添加sku失败'
    })
  }
}

//对外暴漏方法
defineExpose({ initSkuData })
</script>

<style lang="scss" scoped></style>
